﻿/*
 * 设计模式 - 备忘录模式
 * 目的:保持历史记录,随时恢复
 * 三个对象 备忘录  本对象  历史记录
 */

var history = new History();
var user = new User() { Name = "Init" };
history.Add(user.Save());
user.Name = "First Modify";
history.Add(user.Save());
user.Name = "Second Update";
history.Add(user.Save());

var firstUser = history.Get(1);
var originUser = history.Get(0);

Console.WriteLine($"获取第一次修改{firstUser.User.Name}");
Console.WriteLine($"获取原始记录{originUser.User.Name}");


class User
{
    public string Name { get; set; }

    public Momento Save() => new(new() { Name = Name });

    public User Restor(Momento momento) => momento.User;
}

class Momento
{
    public Momento(User user)
    {
        this.User = user;
    }

    public User User { get; set; }
}


class History
{
    public List<Momento> Momentos { get; set; } = new();

    public void Add(Momento momento) => Momentos.Add(momento);

    public Momento Get(int version) => Momentos[version];
}